package com.xuzz.study.thread.lock;

import java.util.concurrent.locks.AbstractQueuedSynchronizer;

public class Mutex {
    class Sync extends AbstractQueuedSynchronizer
    {
        @Override
        protected boolean tryAcquire(int arg) {
            //使用1标识锁定  2表示解锁
            return compareAndSetState(0,1);
        }
        @Override
        protected boolean tryRelease(int arg) {
            setState(0);
            return true;
        }
    }

    Sync sync= new Sync();
    public void lock()
    {
        sync.acquire(0);
    }
    public void  unlock()
    {
        sync.release(0);
    }
}
